package edu.iu.criminfo.voronoi.gpsUtils;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import edu.iu.criminfo.voronoi.cases.Case;
import edu.iu.criminfo.voronoi.init.InitiateCluster;
import edu.iu.criminfo.voronoi.init.ParseDataFile;
import edu.iu.criminfo.voronoi.kmeans.Cluster;
import edu.iu.criminfo.voronoi.object.Edge;
import edu.iu.criminfo.voronoi.object.Point;

public class KMLFileWritter {
	private String filePath;
	private String clusteringAlgorithm;
	private FileOutputStream fOutStream;
	private static String [] markIcons = {"blue_MarkerA", "brown_MarkerB", "darkgreen_MarkerC", "green_MarkerD", "orange_MarkerE", 
											"paleblue_MarkerF", "pink_MarkerG", "purple_MarkerH", "red_MarkerI", "yellow_MarkerJ", 
											"blue_MarkerK", "brown_MarkerL", "darkgreen_MarkerM", "green_MarkerN", "orange_MarkerO",
									        "paleblue_MarkerP", "pink_MarkerQ", "purple_MarkerR", "red_MarkerS", "yellow_MarkerT",
									        "brown_MarkerU", "darkgreen_MarkerV", "green_MarkerW", "orange_MarkerX", "paleblue_MarkerY", "pink_MarkerZ"};

	public KMLFileWritter(String filePath, String clusteringAlgorithm) {
		this.filePath = filePath;
		this.clusteringAlgorithm = clusteringAlgorithm;
	}

	public void writeKMLFileHeader() {
		StringBuilder kmlStr = new StringBuilder();
		kmlStr.append("<kml xmlns=\"http://www.opengis.net/kml/2.2\" " +
				"xmlns:gx=\"http://www.google.com/kml/ext/2.2\" " +
				"xmlns:atom=\"http://www.w3.org/2005/Atom\" " +
				"xmlns:xal=\"urn:oasis:names:tc:ciq:xsdschema:xAL:2.0\">\n");
		kmlStr.append("<Document>\n");
		kmlStr.append("<name>" + clusteringAlgorithm + "</name>\n");
		kmlStr.append("<open>0</open>\n");
		
		for (String mark : markIcons) {
			kmlStr.append("<Style id=\"" + mark + "\">\n");
			kmlStr.append("<IconStyle>\n");
			kmlStr.append("<Icon>\n");
			kmlStr.append("<href>http://www.cs.indiana.edu/~smarru/themis/kml_icons/" + 
					mark + ".png</href>\n");			
			kmlStr.append("</Icon>\n");
			kmlStr.append("</IconStyle>\n");
			kmlStr.append("</Style>\n");
		}
		
		if (!clusteringAlgorithm.isEmpty() && clusteringAlgorithm.equalsIgnoreCase("K-means")) {
			kmlStr.append("<Style id=\"YellowLine\">" +
					"<LineStyle>" +
						"<color>7F0033ff</color>" +
						"<width>10</width>" +
					"</LineStyle>" +
				  "</Style>");
		} else if (!clusteringAlgorithm.isEmpty() && clusteringAlgorithm.equalsIgnoreCase("Modified_K-means")){
			kmlStr.append("<Style id=\"YellowLine\">" +
					"<LineStyle>" +
						"<color>7fff0033</color>" +
						"<width>10</width>" +
					"</LineStyle>" +
				  "</Style>");
		}
		                 

		try {
			fOutStream = new FileOutputStream(filePath);
			fOutStream.write(kmlStr.toString().getBytes());
			fOutStream.flush();
			fOutStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void append(Case aCase, int styleIndex) {
		String kmlStr = "<Placemark>\n" 
			            + "<name></name>\n" 
			            + "<styleUrl>#" + markIcons[styleIndex] + "</styleUrl>\n" 
		                   + "<Point>\n" 
		                       + "<coordinates>\n" 
		                        + aCase.getLocation().coordinateY + "," + aCase.getLocation().coordinateX + "\n"
				               + "</coordinates>\n" 
				           + "</Point>\n" 
				       + "</Placemark>\n";
		try {
			fOutStream = new FileOutputStream(filePath, true);
			fOutStream.write(kmlStr.getBytes());
			fOutStream.flush();
			fOutStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void appendCluster(Cluster cluster, int styleIndex) {
		for (Case aCase: cluster.getCaseList()) {
			append(aCase, styleIndex);
		}
	}
	
	public void appendEdge(Edge edge) {
		StringBuilder kmlStr = new StringBuilder();
		
		kmlStr.append("<Placemark>\n");
		kmlStr.append("<name></name>\n");
		kmlStr.append("<styleUrl>#YellowLine</styleUrl>\n");
		kmlStr.append("<LineString>\n");
		kmlStr.append("<tessellate>1</tessellate>\n");
		//kmlStr.append("<altitudeMode>absolute</altitudeMode>
		kmlStr.append("<coordinates>\n");
		kmlStr.append(edge.begin.coordinateY + ", " + edge.begin.coordinateX + ", 0\n");
		kmlStr.append(edge.end.coordinateY + ", " + edge.end.coordinateX + ", 0\n");
		kmlStr.append("</coordinates>\n");
		kmlStr.append("</LineString>\n");
		kmlStr.append("</Placemark>\n");
		
		try {
			fOutStream = new FileOutputStream(filePath, true);
			fOutStream.write(kmlStr.toString().getBytes());
			fOutStream.flush();
			fOutStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	public void writeKMLFileTail() {
		String kmlStr = "</Document>\n" 
			          + "</kml>\n";
		try {
			fOutStream = new FileOutputStream(filePath, true);
			fOutStream.write(kmlStr.getBytes());
			fOutStream.flush();
			fOutStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

//	public static void main(String[] args) {
//		List<Case> caseList = ParseDataFile.parse("data/vehicle_access.txt");
//		List<Cluster> clusterList = InitiateCluster
//		.generateInitiateCluster(caseList);
//		KMLFileWritter kml = new KMLFileWritter("data/edu.iu.criminfo.voronoi.test.kml", "K-Means");
//		kml.writeKMLFileHeader();
//		int i = 0;
//		for (Cluster cluster : clusterList) {
//			cluster.addCase(new Case(new Point(cluster.getCenter().coordinateX, cluster.getCenter().coordinateY)));
//			kml.appendCluster(cluster, i);
//			i++;
//		}
//		kml.writeKMLFileTail();
//		
//		System.out.println(System.getProperty("user.dir"));
//	}
}
